package cn.com.shopec.erp.stock.excel.impl;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

import cn.com.shopec.erp.common.excel.ExcelBaseExport;
import cn.com.shopec.erp.common.utils.DateUtil;
import cn.com.shopec.erp.stock.vo.StockHistoryVo;

public class StockHistoryExcelExport extends ExcelBaseExport {

	public StockHistoryExcelExport(List<StockHistoryVo> data, String fileName, HttpServletResponse response) {
		super(data, fileName, response);
	}

	@Override
	protected Row setColumnMapping(Object data, Row row) {
		StockHistoryVo stockHistory = (StockHistoryVo) data;
		int cellIndex = 0;
		Cell cell = row.createCell(cellIndex++);
		cell.setCellValue(stockHistory.getProdNo());

		cell = row.createCell(cellIndex++);
		cell.setCellValue(stockHistory.getCommoNo());

		cell = row.createCell(cellIndex++);
		cell.setCellValue(stockHistory.getCommoName());

		cell = row.createCell(cellIndex++);
		cell.setCellValue(stockHistory.getSpecification());

		cell = row.createCell(cellIndex++);
		cell.setCellValue(stockHistory.getSupName());

		cell = row.createCell(cellIndex++);
		cell.setCellValue(stockHistory.getWhName());

		cell = row.createCell(cellIndex++);
		cell.setCellValue(DateUtil.formatTimesTampDate(stockHistory.getCreateTime()));
		
		cell = row.createCell(cellIndex++);
		cell.setCellValue(getStockTypeText(stockHistory.getStockType()));

		cell = row.createCell(cellIndex++);
		cell.setCellValue(stockHistory.getStockNo());
		
		cell = row.createCell(cellIndex++);
		cell.setCellValue(getStockTypeDetailText(stockHistory.getStockType()));

		cell = row.createCell(cellIndex++);
		cell.setCellValue(stockHistory.getOriginalQuantity());

		cell = row.createCell(cellIndex++);
		cell.setCellValue(getChangeQuantity(stockHistory));

		cell = row.createCell(cellIndex++);
		cell.setCellValue(getStockNowQuantity(stockHistory));
		
		return row;
	}

	@Override
	protected List<String> setExcelTitleValue() {
		List<String> title = new ArrayList<String>();
		title.add("货品编号");
		title.add("商品编号");
		title.add("商品名称");
		title.add("货品规格");
		title.add("供应商");
		title.add("仓库");
		title.add("出入库时间");
		title.add("出入库");
		title.add("出入库单号");
		title.add("出入库类型");
		title.add("原库存数量");
		title.add("出入数量");
		title.add("现库存数量");
		return title;
	}

	private String getStockTypeText(Integer stockType) {
		if (stockType.intValue() > 0) {
			return "入库";
		} else if (stockType.intValue() < 0) {
			return "出库";
		}
		return "";
	}

	/**
	 * 设置出库类型
	 * @param stockType
	 * @return
	 */
	private String getStockTypeDetailText(Integer stockType) {
		if (stockType.equals(1)) {
			return "货品采购入库";
		} else if (stockType.equals(2)) {
			return "订单退换货入库";
		} else if (stockType.equals(3)) {
			return "次品换货入库";
		} else if (stockType.equals(4)) {
			return "借出归还入库";
		} else if (stockType.equals(5)) {
			return "调拨入库";
		} else if (stockType.equals(6)) {
			return "其他入库";
		} else if (stockType.equals(-1)) {
			return "包裹销售出库";
		} else if (stockType.equals(-2)) {
			return "订单退货出库";
		} else if (stockType.equals(-3)) {
			return "次品退货出库";
		}
		return "";
	}

	/**
	 * 设置显示正负数
	 * @param stockHistory
	 * @return
	 */
	private Long getChangeQuantity(StockHistoryVo stockHistory) {
		Integer stockType  = stockHistory.getStockType();
		if(stockType != null){
			if (stockType.intValue() > 0) {
				return stockHistory.getChangeQuantity();
			} else if (stockType.intValue() < 0) {
				return -stockHistory.getChangeQuantity();
			}
		}
		return 0L;
	}
	
	/**
	 * 计算数量
	 * @param stockHistory
	 * @return
	 */
	private Long getStockNowQuantity(StockHistoryVo stockHistory) {
		Integer stockType = stockHistory.getStockType();
		Long originalQuantity = stockHistory.getOriginalQuantity();
		Long changeQuantity = stockHistory.getChangeQuantity();
		if (stockType != null && originalQuantity != null && changeQuantity != null) {
			if (stockType.intValue() > 0) {
				return originalQuantity + changeQuantity;
			} else if (stockType.intValue() < 0) {
				return originalQuantity - changeQuantity;
			}
		}
		return 0L;
	}
}
